package com.gurumades.jsonform.client;

import java.util.ArrayList;

import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.gurumades.jsonform.client.presenter.JsonFormPresenter;


public class AppController implements IFormController {
	 private HasWidgets container;
	 private JsonFormPresenter jsonPresenter;
	 private JSONObject displayObject;
	 
	 private IFormController appController;
	 private String formID;
	 
	 public AppController(JSONObject displayObject) {
		 this.displayObject = displayObject;
	 }
	
	public void go(HasWidgets container, IFormController appController, String formID) {
		this.container = container;
		this.appController = appController;
		this.formID = formID;
		
		doJsonForm();
	}

	public void doJsonForm(){
		if (jsonPresenter == null) {
			jsonPresenter = new JsonFormPresenter(new JsonFormView(),  displayObject, appController, formID);
		}
		jsonPresenter.go(container, this);
	}
	
	
	public ArrayList<JSONObject> flush(){
		return jsonPresenter.flush();
	}

	public boolean hasErrors(){
		return false;
	}

	public JSONValue getDisplayFields(String formID) {
		return appController.getDisplayFields(formID);
	}
	
	@Override
	public String getUserRole() {
		return appController.getUserRole();
	}
}
